#! /usr/bin/python
# -*- coding:utf8 -*- 

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from mainGraphics import MainView
# from spline import Spline

class MainWidget(QWidget):
    def __init__(self, parent=None):
        """构造函数"""
        super(MainWidget, self).__init__(parent)
        self.__initObjects()
        self.__setObjects()
        self.__setObjectsStyle()
        self.__setLayoutRelation()
        self.updateLabel()
        self.setWindowIcon(QIcon(QPixmap('./1.png')))

        self.start_btn.clicked.connect(self.start)
        self.get_points_btn.clicked.connect(self.get_points)
        self.drawline_btn.clicked.connect(self.drawline)
        self.connect(self.tensionSlider, SIGNAL("valueChanged(int)"), self.set_tension)
        self.connect(self.grainSlider, SIGNAL("valueChanged(int)"), self.set_grain)
        self.connect(self.speedCombo, SIGNAL("currentIndexChanged(int)"),
                                             self.set_speed)

    def __initObjects(self):
        """初始化部件和部分变量"""
        self.speed_kind = 1
        self.haveGotPoint = False
        self.tensionSlider = QSlider(Qt.Horizontal)
        self.grainSlider = QSlider(Qt.Horizontal)
        self.speedCombo = QComboBox()
        self.infoLabel = QLabel()
        self.__layout = QHBoxLayout()
        self.__left_form_layout = QFormLayout()
        self.__left_layout = QVBoxLayout()
        self.__right_layout = QVBoxLayout()
        self.start_btn = QPushButton(u"动起来")
        self.get_points_btn = QPushButton(u"开始获取点")
        self.drawline_btn = QPushButton(u'画线')
        self.__view = MainView()

    def __setObjects(self):
        """设置部件属性"""
        left_width = 160
        self.tensionSlider.setRange(1,10)
        self.grainSlider.setRange(0,100)
        self.tensionSlider.setValue(6)
        self.grainSlider.setValue(10)
        self.tensionSlider.setToolTip(u'改变tension')
        self.grainSlider.setToolTip(u'改变grain')
        self.speedCombo.addItem("S = 2T - T^2")
        self.speedCombo.addItem("S = T^3")
        self.speedCombo.addItem("S = (2t-1)^3/2+1/2")
        self.infoLabel.setIndent(5)
        self.infoLabel.setWordWrap(True)
        self.infoLabel.setMargin(5)
        self.infoLabel.setFixedWidth(left_width)
        self.start_btn.setFixedSize(left_width,48)
        self.get_points_btn.setFixedSize(left_width,48)
        self.drawline_btn.setFixedSize(left_width,48)
        # self.tensionSlider.setFixedSize(left_width,48)
        # self.grainSlider.setFixedSize(left_width,48)
        self.__left_layout.setContentsMargins(0,0,0,0)
        self.__right_layout.setContentsMargins(0,0,0,0)
        self.__layout.setContentsMargins(0,0,0,0)
        self.__layout.setSpacing(0)
        self.setFixedSize(800,512)
    def __setObjectsStyle(self):
        """设置按钮等部件样式"""
        start = '''
            QPushButton{
                outline: none;
                background-color: #002b36;
                border-radius: 0px;
                color: white;
                }
            QPushButton:Hover{
                color: #CC9999;
                }
            QPushButton:pressed{
                background-color: #CC3333;
                }
        '''
        info = '''
            QLabel{
                background-color: #CC9999;
                border-radius: 0px;
                color: black;
                }
        '''
        self.start_btn.setStyleSheet(start)
        self.drawline_btn.setStyleSheet(start)
        self.infoLabel.setStyleSheet(info)
        self.get_points_btn.setStyleSheet(start)

    def __setLayoutRelation(self):
        """设置界面layout"""
        self.__left_form_layout.addRow(u"Grain : ", self.grainSlider)
        self.__left_form_layout.addRow(u"Tension : ", self.tensionSlider)
        self.__left_layout.addWidget(self.get_points_btn)
        self.__left_layout.addWidget(self.drawline_btn)
        self.__left_layout.addWidget(self.start_btn)
        self.__left_layout.addSpacing(10)
        self.__left_layout.addLayout(self.__left_form_layout)
        self.__left_layout.addWidget(self.speedCombo)

        # self.__left_layout.addWidget(self.tensionSlider)
        # self.__left_layout.addWidget(self.grainSlider)

        self.__left_layout.addStretch(1)
        self.__left_layout.addWidget(self.infoLabel)

        self.__right_layout.addWidget(self.__view)
        self.__layout.addLayout(self.__left_layout)
        self.__layout.addLayout(self.__right_layout)
        self.setLayout(self.__layout)
        pass

    def start(self):
        '''
        1. get point finish
        2. draw points and generate a spline
        3. let the item run
        '''
        self.__view.get_points_finish()
        if not self.__view.hasSpline():
            self.__view.drawSpline(self.speed_kind)
        self.__view.runPerson()

    def get_points(self):
        self.get_points_btn.setText(u'重新获取点')
        self.__view.get_points()

    def set_tension(self, num):
        """设置伸缩度"""
        self.__view.tension = float(num)/10
        if self.__view.hasSpline():
            self.__view.drawSpline(self.speed_kind)
        self.updateLabel()

    def set_speed(self, num):
        """设置速度曲线"""
        self.speed_kind = num + 1
        if self.__view.hasSpline():
            self.__view.drawSpline(self.speed_kind)

    def set_grain(self, num):
        """设置平滑度"""
        self.__view.grain = num
        if self.__view.hasSpline():
            self.__view.drawSpline(self.speed_kind)
        self.updateLabel()

    def drawline(self):
        """调用view的drawSpline,最终调用scene的drawSpline"""
        self.__view.drawSpline(self.speed_kind)

    def updateLabel(self):
        """更新信息标签"""
        text = ''
        text = text + u'tension:' + str(float(self.tensionSlider.value())/10) + '\n'
        text = text + u'grain:' + str(self.grainSlider.value()) + '\n'
        text = text + u'distance:' + str(self.speedCombo.itemText(self.speed_kind)) + '\n'
        if self.__view.grain < 30:
            text = text + u'grain太小，小车可能会出现意外翻转'
        self.infoLabel.setText(text)

if __name__ == "__main__":
    import os
    import sys
    # os.chdir(sys.path[0])
    app = QApplication(sys.argv)
    mw = MainWidget()
    mw.show()
    sys.exit(app.exec_())
